<?php

namespace catchAdmin\apidoctor\controller;

use app\common\business\Contract;
use app\common\lib\ZhyxMongo;
use app\common\model\Suggetion;
use catcher\exceptions\FailedException;
use think\Request;
use catcher\CatchResponse;
use catcher\base\CatchRequest;
use catcher\base\CatchController;
use think\exception\ValidateException;
use catchAdmin\apidoctor\validate\DoctorValidate;
use catchAdmin\permissions\model\Users as UsersModel;

class Doctor extends CatchController
{
    /**
     * 我的
     * @param CatchRequest $request
     * @return \think\response\Json
     */
    public function get_yhd_my(CatchRequest $request): \think\response\Json
    {
        return CatchResponse::success((new UsersModel)->getYhdDoctor(request()->userInfo['doctor_code']));
    }

    /**
     * 生成票据
     * @param Request $request
     * @return \think\response\Json
     */
    public function get_pj(Request $request): \think\response\Json
    {
        $req = $request->param();
        $ticket = hash("sha256", time()) . base64_encode(json_encode($req));
        return CatchResponse::success(['ticket' => $ticket]);
    }


    public function getMyServicesSigned(Request $request): \think\response\Json
    {
        $limit = $request->param("limit", 15, "int");

        $result = (new \app\common\model\Contract())->where([["contract_doctor.doctor_id", "=", $request->user()->id]])->paginate($limit);

        if (count($result->items()) > 0) {
            $result->visible(["id", "service_info", "contract_doctor", "name"])->append(["service_day"]);
        }
        //todo  明日处理这里
        return CatchResponse::paginate($result);


    }

    public function doctorSuggestion(Request $request): \think\response\Json
    {
        $guid = $request->param("guid", "", "string");
        $patientId = $request->param("patient_id", "", "string");
        $suggestion = $request->param("suggestion", "", "string");
        if (empty($guid)) {
            throw new FailedException("请传入周分组id");
        }
        if (empty($patientId)) {
            throw new FailedException("患者id不能为空");
        }
        if (empty($suggestion)) {
            throw new FailedException("请输入健康周报");
        }
        //判断当前患者是否有建议存在
        $isExists = ZhyxMongo::getInstance()->setCollection("suggestion")->findOne(["patient_id" => $patientId, "suggestion.guid" => $guid]);
        if (empty($isExists)) {
            $result = ZhyxMongo::getInstance()->setCollection('suggestion')->updateOne(["patient_id" => $patientId], [
                '$set' => ['patient_id' => $patientId],
                '$push' => ["suggestion" => ["guid" => $guid, "suggestion" => $suggestion]]
            ], ['upsert' => true]);
        } else {
            $result = ZhyxMongo::getInstance()->setCollection('suggestion')->updateOne(['suggestion.guid' => $guid], [
                '$set' => [
                    'suggestion.$.suggestion' => $suggestion
                ]]);
        }
        if ($result->getMatchedCount() > 0 || $result->getModifiedCount() || $result->getUpsertedCount() > 0) {
            return CatchResponse::success();
        }
        return CatchResponse::fail("发布失败");

    }

    public function getSuggestion(Request $request): \think\response\Json
    {
        $guid = $request->param("guid", "", "string");
        if (empty($guid)) {
            throw new FailedException("Guid不能为空");
        }
        $collection = ZhyxMongo::getInstance()->setCollection("suggestion");
        $cursor = $collection->aggregate([['$unwind' => ['path' => '$suggestion']], ['$group' => ['_id' => '$suggestion.guid', 'suggestion' => ['$last' => '$suggestion.suggestion']]], ['$match' => ['_id' => $guid]], ['$project' => ['suggestion_id' => '$_id', 'suggestion' => true, '_id' => false]]]);
        $suggestion = [];
        foreach ($cursor as $k => $v) {
            $suggestion = array($v);
        }
        return CatchResponse::success($suggestion);

    }

}